<template>
  <view>
    <!-- 商品列表数据 -->
    <view class="goods-list">
      <!-- 循环渲染 -->
      <view v-for="(good, i) in goodsList" :key='i' @click="gotodetail(good)">
        <my-goods :good="good"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj:{
          // 搜索关键词
          query: '',
          // 搜索分类id
          cid: '',
          // 商品列表的页数与每一页展示的商品数
          pagenum: 1,
          pagesize: 10
        },
        // 商品列表数据
        goodsList:[],
        // 总数量 用来实现分页
        total: 0,
        // 节流阀
        isloading:false
      };
    },
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      
      this.getGoodsList()
    },
    methods:{
      async getGoodsList(cb){
        // 打开节流阀
        this.isloading = true
        const {data: res} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        if(res.meta.status !== 200) return uni.$showMsg()
        // 关闭节流阀
        this.isloading = false
        cb && cb()
        this.goodsList = [...this.goodsList,...res.message.goods]
        this.total = res.message.total   
      },
      gotodetail(good){
        uni.navigateTo({
          url:'/subpkg/good_detail/good_detail?goods_id=' + good.goods_id
        })
      }
    },
    onReachBottom() {
      if(this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg("数据加载完毕！没有更多了~")
      // 判断节流阀状态
      if(this.isloading) return 
      this.queryObj.pagenum++
      this.getGoodsList()
    },
    onPullDownRefresh(){
      this.total = 0
      this.queryObj.pagenum = 1
      this.isloading = false
      this.goodsList = []
      
      this.getGoodsList(()=> uni.stopPullDownRefresh())   
    }
  }
</script>

<style lang="scss">

</style>
